package com.example.demo.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.custom.AjaxResult;
import com.example.demo.entity.NewCar1;
import com.example.demo.entity.Position;
import com.example.demo.entity.SysDept;
import com.example.demo.entity.SysUser;
import com.example.demo.entity.ThreadInput;
import com.example.demo.entity.ThreadInput2;
import com.example.demo.entity.UsedCar1;
import com.example.demo.form.ThreadInputForm;
import com.example.demo.security.UserUtils;
import com.example.demo.service.NewCar1Service;
import com.example.demo.service.PositionService;
import com.example.demo.service.SysDeptService;
import com.example.demo.service.SysUserService;
import com.example.demo.service.ThreadInput2Service;
import com.example.demo.service.ThreadInputService;
import com.example.demo.service.UsedCar1Service;

@RestController
@RequestMapping(value = "/threadInput2")
public class ThreadInput2Controller {
	@Autowired
	private NewCar1Service carService;
	@Autowired
	private UsedCar1Service oldcarService;
	@Autowired
	private ThreadInput2Service service2;
	@Autowired
	private SysDeptService deptService;
	@Autowired
	private PositionService postiService;
	@Autowired
	private ThreadInputService service1;
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private SysUserService sysuserService;

	private DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");

	@RequestMapping(value = "/save")
	private Object save2(ThreadInputForm Form) throws ParseException {
		Integer saleId1 = Form.getId();
		Date d = new Date();
		SysUser user = userUtils.getUser();
		SysDept dept = deptService.findById(Form.getDeptId());
		Position zhiwei = postiService.findById(Form.getZhiweiId());
		Date d1 = dateFormat1.parse("1999-01-01");
		ThreadInput sale1 = new ThreadInput();
		ThreadInput Sale1 = new ThreadInput();
		ThreadInput2 sale2 = new ThreadInput2();
		ThreadInput2 Sale2 = new ThreadInput2();
		if (saleId1 == null) {
			Sale1.setName(Form.getName());
			Sale1.setCustomerName(Form.getCustomerName());
			Sale1.setUserId(user);
			sale1 = service1.save(Sale1);
		} else {
			sale1 = service1.findById(saleId1);
			sale1.setName(Form.getName());
			Sale1.setCustomerName(Form.getCustomerName());
			Sale1.setUserId(user);
			service1.save(sale1);
			List<ThreadInput2> list = service2.findByThread(sale1);
			for (ThreadInput2 ThreadInput2 : list) {
				if (d1.compareTo(ThreadInput2.getBeginDate()) == -1) {
					d1 = ThreadInput2.getBeginDate();
					Sale2 = ThreadInput2;
				}
			}
			Sale2.setEndDate(d);
			service2.save(Sale2);

		}
		if (Form.getCars().length() > 0) {

			List<NewCar1> cars = new ArrayList<>();
			String car = Form.getCars();
			for (String name : car.split(",")) {
				cars.add(carService.findById(Integer.parseInt(name)));
			}
			sale2.setCars(cars);
		}
		if (Form.getCars2().length() > 0) {

			List<NewCar1> cars2 = new ArrayList<>();
			String car2 = Form.getCars();
			for (String name : car2.split(",")) {
				cars2.add(carService.findById(Integer.parseInt(name)));
			}
			sale2.setCars2(cars2);
		}
		if (Form.getOldcars1().length() > 0) {

			List<UsedCar1> cars = new ArrayList<>();
			String car = Form.getOldcars1();
			for (String name : car.split(",")) {
				cars.add(oldcarService.findById(Integer.parseInt(name)));
			}
			sale2.setOldcars1(cars);
		}
		if (Form.getOldcars2().length() > 0) {

			List<UsedCar1> cars = new ArrayList<>();
			String car = Form.getOldcars2();
			for (String name : car.split(",")) {
				cars.add(oldcarService.findById(Integer.parseInt(name)));
			}
			sale2.setOldcars2(cars);
		}

		BeanUtils.copyProperties(Form, sale2, "id", "userId", "cars", "cars2","oldcars1","oldcars2");
		sale2.setThread(sale1);
		sale2.setDept(dept);
		sale2.setZhiwei(zhiwei);
		sale2.setBeginDate(d);
		service2.save(sale2);
		return new AjaxResult("数据保存成功");
	}

	@RequestMapping(value = "/findByUserId")
	public Object findByUserId(ThreadInputForm Form) {
		List<ThreadInput> thread = new ArrayList<ThreadInput>();
		Integer id = Form.getUserId();
		if (id != null) {
			SysUser user = sysuserService.findById(id);
			thread = service1.findByUserId(user);
		}

		return new AjaxResult(thread);
	}

	@RequestMapping(value = "/findById")
	public Object findbyId(Integer id) throws ParseException {
		ThreadInput thread = service1.findById(id);
		List<ThreadInput2> list = service2.findByThread(thread);
		DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
		Date d = dateFormat1.parse("1999-01-01");
		ThreadInput2 yan = new ThreadInput2();
		for (ThreadInput2 y : list) {
			if (d.compareTo(y.getBeginDate()) == -1) {
				d = y.getBeginDate();
				yan = y;
			}
		}
		return new AjaxResult(yan);
	}

}
